package com.sz.web.jackson.databind;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Date反序列化
 *
 * @author: yxl
 * 2025-07-23
 */
public class DateDeserializer extends JsonDeserializer<Date> {
    public static final DateDeserializer INSTANCE = new DateDeserializer();
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Date deserialize(JsonParser parser, DeserializationContext deserializationContext) throws IOException, JacksonException {
        try {
            return DATE_FORMAT.parse(parser.getValueAsString());
        } catch (Exception e) {
            throw new IOException("无法解析日期: " + parser.getValueAsString(), e);
        }
    }
}
